package com.reggie.controller;

import com.reggie.pojo.Employee;
import com.reggie.service.EmployeeService;
import com.reggie.util.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

//员工请求映射类
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    //员工登录
    @PostMapping("/login")
    public ResultUtil<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        Employee employee1 = employeeService.login(employee.getUsername(), employee.getPassword());
        if (employee1 == null) return ResultUtil.error("用户名或密码有误！");
        Map<String, Object> map = new HashMap<>();
        map.put("id", String.valueOf(employee1.getId()));
        String token = JwtUtil.genToken(map);
        stringRedisTemplate.opsForValue().set(token, token, 30, TimeUnit.MINUTES);
        request.getSession().setAttribute("userInfo1", token);
        return ResultUtil.success(employee1);
    }

    //员工退出登录
    @PostMapping("/logout")
    public ResultUtil<Null> logout(HttpServletRequest request) {
        String token = (String) request.getSession().getAttribute("userInfo1");
        stringRedisTemplate.opsForValue().getOperations().delete(token);
        request.getSession().removeAttribute("userInfo1");
        return ResultUtil.success(null);
    }

    //分页查询多条员工信息
    @GetMapping("/page")
    public ResultUtil<PageUtil<Employee>> page(@RequestParam(value = "page", defaultValue = "0") int page,
                                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                               @RequestParam(value = "name", required = false) String name) {
        PageUtil<Employee> employeePage = employeeService.getEmployeeList(page, pageSize, name);
        if (employeePage == null) return ResultUtil.error("未查询到员工！");
        return ResultUtil.success(employeePage);
    }

    //工具id查询单条员工信息
    @GetMapping("/{id}")
    public ResultUtil<Employee> get(@PathVariable("id") Long id) {
        Employee employee = employeeService.getEmployeeById(id);
        if (employee == null) return ResultUtil.error("未查询到员工！");
        return ResultUtil.success(employee);
    }

    //添加员工
    @PostMapping
    public ResultUtil<Null> add(@RequestBody Employee employee) {
        Map<String, Object> map = ThreadLocalUtil.get();
        employee.setCreateUser(Long.valueOf((String) map.get("id")));
        employee.setUpdateUser(Long.valueOf((String) map.get("id")));
        Long id = new SnowflakeIdWorkerUtil(0, 0).nextId();
        employee.setId(id);
        if (employeeService.addEmployee(employee)) return ResultUtil.success(null);
        return ResultUtil.error("添加员工失败！");
    }

    //修改员工信息
    @PutMapping
    public ResultUtil<Null> edit(@RequestBody Employee employee) {
        Map<String, Object> map = ThreadLocalUtil.get();
        employee.setUpdateUser(Long.valueOf((String) map.get("id")));
        if (employeeService.editEmployee(employee)) return ResultUtil.success(null);
        return ResultUtil.error("修改员工失败！");
    }
}
